package com.test.testopengl;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;

/**
 * This is a port of the {@link http://nehe.gamedev.net} OpenGL tutorials to the
 * Android 1.5 OpenGL ES platform. Thanks to NeHe and all contributors for their
 * great tutorials and great documentation. This source should be used together
 * with the textual explanations made at {@link http://nehe.gamedev.net}. The
 * code is based on the original Visual C++ code with all comments made. It has
 * been altered and extended to meet the Android requirements. The Java code has
 * according comments.
 * 
 * If you use this code or find it helpful, please visit and send a shout to the
 * author under {@link http://www.insanitydesign.com/}
 * 
 * @DISCLAIMER This source and the whole package comes without warranty. It may
 *             or may not harm your computer or cell phone. Please use with
 *             care. Any damage cannot be related back to the author. The source
 *             has been tested on a virtual environment and scanned for viruses
 *             and has passed all tests.
 * 
 * 
 *             This is an interpretation of "Lesson 03: Adding Color" for the
 *             Google Android platform.
 * 
 * @author Savas Ziplies (nea/INsanityDesign)
 */
public class MyOpenGlRenderer implements Renderer {

	float[] vertices = null;
	ByteBuffer byteBuf;
	FloatBuffer vertexBuffer;
	int mRangle = 0;
	float mRotatefX = 0f;
	float mRotatefY = 0f;
	float mRotatefZ = 0f;
	private Arm arm;

	/* The buffers for our light values ( NEW ) */
	private FloatBuffer lightAmbientBuffer;
	private FloatBuffer lightDiffuseBuffer;
	private FloatBuffer lightPositionBuffer;

	/**
	 * Instance the Triangle and Square objects
	 * 
	 * @throws IOException
	 */
	public MyOpenGlRenderer(String path) throws IOException // 构造函数
	{
		arm = new Arm(path);

		float[] lightAmbient = { 0.5f, 0.5f, 0.5f, 1.0f };
		float[] lightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
		float[] lightPosition = { 0.0f, 0.0f, 2.0f, 1.0f };

		//
		ByteBuffer byteBuf = ByteBuffer.allocateDirect(lightAmbient.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		lightAmbientBuffer = byteBuf.asFloatBuffer();
		lightAmbientBuffer.put(lightAmbient);
		lightAmbientBuffer.position(0);

		byteBuf = ByteBuffer.allocateDirect(lightDiffuse.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		lightDiffuseBuffer = byteBuf.asFloatBuffer();
		lightDiffuseBuffer.put(lightDiffuse);
		lightDiffuseBuffer.position(0);

		byteBuf = ByteBuffer.allocateDirect(lightPosition.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		lightPositionBuffer = byteBuf.asFloatBuffer();
		lightPositionBuffer.put(lightPosition);
		lightPositionBuffer.position(0);

	}

	/**
	 * The Surface is created/init()
	 */
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {

		// And there'll be light!
		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbientBuffer); // Setup
																			// The
																			// Ambient
																			// Light
																			// (
																			// NEW
																			// )
		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuseBuffer); // Setup
																			// The
																			// Diffuse
																			// Light
																			// (
																			// NEW
																			// )
		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPositionBuffer); // Position
																				// The
																				// Light
																				// (
																				// NEW
																				// )
		gl.glEnable(GL10.GL_LIGHT0); // Enable Light 0 ( NEW )

		// Settings
		gl.glDisable(GL10.GL_DITHER); // Disable dithering ( NEW )
		gl.glEnable(GL10.GL_TEXTURE_2D); // Enable Texture Mapping
		gl.glShadeModel(GL10.GL_SMOOTH); // Enable Smooth Shading
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // set Background 设置背景颜色
		gl.glClearDepthf(1.0f); // Depth Buffer Setup
		gl.glEnable(GL10.GL_DEPTH_TEST); // Enables Depth Testing
		gl.glDepthFunc(GL10.GL_LEQUAL); // The Type Of Depth Testing To Do

		// Really Nice Perspective Calculations
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
	}

	// 设置选装角度
	public void setRotaef(int rangle, float rotatefX, float rotatefY,
			float rotatefZ) {
		mRangle = rangle + mRangle;
		mRotatefX = rotatefX;
		mRotatefY = rotatefY;
		mRotatefZ = rotatefZ;
	}

	/**
	 * Here we do our drawing
	 */
	public void onDrawFrame(GL10 gl) {

		// Clear Screen And Depth Buffer
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity(); // Reset The Current Modelview Matrix
		gl.glTranslatef(1.0f, 0.0f, -500.0f); // Move down 1.2 Unit And Into
		// 旋转3D模型
		gl.glRotatef(mRotatefY, 1f, 0f, 0f);
		gl.glRotatef(mRotatefX, 0f, 1f, 0f);

		arm.draw(gl);
		gl.glTranslatef(-1.0f, 0.0f, -500.0f); // Move down 1.2 Unit And Into
		// The Screen 6.0
		gl.glEnable(GL10.GL_LIGHTING);// 开启照明
		gl.glEnable(GL10.GL_BLEND);
		// Log.e("World",
		// "length:"+vertices.length+"vaule"+vertices[0]+" "+vertices[1]);
	}

	/**
	 * If the surface changes, reset the view
	 */
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		if (height == 0) { // Prevent A Divide By Zero By
			height = 1; // Making Height Equal One
		}

		gl.glViewport(0, 0, width, height); // Reset The Current Viewport
		gl.glMatrixMode(GL10.GL_PROJECTION); // Select The Projection Matrix
		gl.glLoadIdentity(); // Reset The Projection Matrix

		// Calculate The Aspect Ratio Of The Window
		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
				20000.0f);

		gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix
		gl.glLoadIdentity(); // Reset The Modelview Matrix
	}
}
